package com.situ.studentmanagement.controller;


import com.octo.captcha.Captcha;
import com.situ.studentmanagement.Constants;

import com.situ.studentmanagement.dao.AfficheMapper;
import com.situ.studentmanagement.dao.GoodsMapper;
import com.situ.studentmanagement.dao.OrderMapper;
import com.situ.studentmanagement.dao.UserMapper;
import com.situ.studentmanagement.model.Affiche;
import com.situ.studentmanagement.model.Goods;
import com.situ.studentmanagement.model.Orderone;
import com.situ.studentmanagement.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Null;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.situ.studentmanagement.Constants.*;


/**
 * Created by niugao on 6/13/2016.
 */
@Controller //表示这个类是一个Controller，于是当请求路径 / 时，这个Contrller的实例被自动创建
@RequestMapping("/") //这个controller对应到路径 /
public class MainController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    AfficheMapper afficheMapper;
    @Autowired
    private OrderMapper orderMapper;

    @RequestMapping(method = RequestMethod.GET)
    public String index(HttpSession session, Model model) {

        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }
        //查询说有的菜品
        List<Goods> goodses=goodsMapper.selectAllGoods();
        model.addAttribute("goods",goodses);
        //查询所有的公告信息
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);
        return "index";
    }


    @RequestMapping(method = RequestMethod.POST)
    public String login(HttpSession session,Model model,
                        @RequestParam("name")String name,
                        @RequestParam("password") String password,
                        @RequestParam("role") String role,
                        @RequestParam("random") String random) {
        String errMsg = "";
        int loginID = 0;

        if (role.equals("登录")) {
            Captcha captcha = (Captcha) session.getAttribute("captcha");
            if (!captcha.validateResponse(random)) {
                //验证码错误！
                errMsg = "验证码输的不对呀!";
                model.addAttribute("value_name", name);
                model.addAttribute("value_password", password);
                model.addAttribute("errMsg", errMsg);
                return "left"; //返回前台文件件的名字。
            }
            try {
                User user = this.userMapper.login(name, password);
                //把id保存到session中
                session.setAttribute(USER_NAME,user.getName());
                session.setAttribute(USER_ID,user.getId());
                model.addAttribute("message",1);
                model.addAttribute("name",user.getName());
                session.setAttribute(USER_PASSWORD,user.getPassword());
                model.addAttribute("user", user);
                //查询说有的菜品
                List<Goods> goodses=goodsMapper.selectAllGoods();
                model.addAttribute("goods",goodses);
                //查询所有的公告信息
                List<Affiche> affiches=afficheMapper.selectAllAffiche();
                model.addAttribute("affiches",affiches);
                //生成用户的订单,并取得订单的订单编号
                Date date =new Date();
                long orderid=date.getTime();
                Orderone orderone=new Orderone(0,orderid,user.getId(),user.getName(),user.getPhone(),user.getAddress(),"否",0,0,null,0);
                orderMapper.inserOrderOne(orderone);
                int orderId=orderone.getId();
                session.setAttribute(ORDER_ID,orderId);
                session.setAttribute(ORDER_NUMBER,orderid);
                return "index";
            } catch (Exception e) {
                errMsg = "用户名或密码不存在";
                model.addAttribute("errMsg", errMsg);
                return "index"; //返回前台文件件的名字。
            }
        } else {
            return "register";
        }
    }
}